package com.bullbuddy.dao;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.bullbuddy.po.CofSigning;

public class CofSigningDAO extends AbstractDAO<CofSigning> {

	public CofSigning findById(String cofId, String staffId) {
		List<CofSigning> list = this.find(cofId, staffId, null);
		if (list != null && list.size() > 0)
			return list.get(0);
		return null;
	}

	public List<CofSigning> find(String cofId, String staffId,
			String departmentId) {
		List<CofSigning> cfSignList = new ArrayList<CofSigning>();
		try {
			// open the connetction
			this.openConnection();
			// call the procedure
			CallableStatement proc = this.conn
					.prepareCall("{call cofSigning_query(?,?,?)}");
			// set the params
			proc.setString("cofId", cofId);
			proc.setString("staffId", staffId);
			proc.setString("departmentId", departmentId);
			// execute the procedure
			ResultSet rs = proc.executeQuery();
			if (rs != null) {
				while (rs.next()) {
					CofSigning cfSign = new CofSigning();
					cfSign.setCofId(rs.getString("cofId"));
					cfSign.setStaffId(rs.getString("staffId"));
					cfSign.setStaffName(rs.getString("staffName"));
					cfSign.setDepartmentId(rs.getString("departmentId"));
					cfSign.setSigningTime(rs.getTimestamp("signingTime"));
					cfSign.setLateTime((Integer.parseInt(rs
							.getString("lateTime"))));
					cfSign.setSigningFlag(rs.getString("signingFlag"));
					// cfSign.setReserve(rs.getBytes("reserve"));
					cfSignList.add(cfSign);
				}
				return cfSignList;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				this.closeConnection();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	@Override
	public List<CofSigning> findAll() {
		return find(null, null, null);
	}

	public List<CofSigning> findByCofId(String cofId) {
		return find(cofId, null, null);
	}
}
